package com.kejiguan.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.kejiguan.ui.adapter.SelectSheduleObjectAdapter;
import com.kejiguan.ui.model.DisplayArea;
import com.kejiguan.ui.model.ExhibitionNeItem;
import com.kejiguan.ui.model.SelectSheduleObject;
import com.kejiguan.ui.model.SubSelectSheduleObject;
import com.kejiguan.ui.util.CacheDataUtil;
import com.kejiguan.ui.widget.MyLinearLayoutManager;
import com.kejiguan.ui.widget.SpacesItemDecoration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by YYY on 16/8/16.
 */
public class SelectSheduleObjectActivity extends Activity implements View.OnClickListener,SelectSheduleObjectAdapter.OnAllSelectListener{

    private View confirmBtn;
    private RecyclerView selectSheduleObjectList;
    private List<SelectSheduleObject> selectSheduleObjects;
    private String selectIds="";
    private SelectSheduleObjectAdapter adapter;

    private int selectColor,unSelectColor;

    private boolean isAllSelect=false;
    private View allSelectLayout;
    private ImageView allSelectImg;
    private TextView allSelectTxt;

    @Override
    public void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        setContentView(R.layout.activity_select_timing_shedule_object_layout);
        selectColor=getResources().getColor(R.color.text_color2);
        unSelectColor=getResources().getColor(R.color.text_color1);
        getIntentData();
        initDatas();
        initViews();
    }

    private void getIntentData()
    {
        selectIds=getIntent().getExtras().getString("selectIds");
        if (!TextUtils.isEmpty(selectIds)&&selectIds.contains("ZALL"))
        {
            isAllSelect=true;
        }else
        {
            isAllSelect=false;
        }
    }


    private void initViews()
    {
        allSelectLayout=findViewById(R.id.all_select_state_layout);
        allSelectImg= (ImageView) findViewById(R.id.all_select_state_img);
        allSelectTxt= (TextView) findViewById(R.id.all_select_state_txt);

        if (!TextUtils.isEmpty(selectIds)&&selectIds.contains("ZALL"))
        {
            allSelectImg.setImageResource(R.mipmap.kjg_menu_checked);
            allSelectTxt.setTextColor(selectColor);
            allSelectTxt.setBackgroundResource(R.drawable.kjg_item_display_bg2);
        }else
        {
            allSelectImg.setImageResource(R.mipmap.kjg_menu_uncheck);
            allSelectTxt.setTextColor(unSelectColor);
            allSelectTxt.setBackgroundResource(R.drawable.kjg_item_display_bg1);
        }

        confirmBtn=findViewById(R.id.confirm_btn);
        confirmBtn.setOnClickListener(this);
        allSelectLayout.setOnClickListener(this);
        selectSheduleObjectList= (RecyclerView) findViewById(R.id.object_List);
        MyLinearLayoutManager layoutManager=new MyLinearLayoutManager(SelectSheduleObjectActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        selectSheduleObjectList.setLayoutManager(layoutManager);
        selectSheduleObjectList.addItemDecoration(new SpacesItemDecoration(80,false));
        adapter=new SelectSheduleObjectAdapter(SelectSheduleObjectActivity.this,selectSheduleObjects);
        adapter.setOnAllSelectListener(SelectSheduleObjectActivity.this);
        selectSheduleObjectList.setAdapter(adapter);


    }

    private void initDatas()
    {
        selectSheduleObjects=new ArrayList<>(0);
        List<DisplayArea> displayAreas = CacheDataUtil.getInstance().getDisplayAreas();
        if (!TextUtils.isEmpty(selectIds)&&selectIds.contains("ZALL"))
        {

            for (int i = 0; i < displayAreas.size(); i++) {

                DisplayArea displayArea = displayAreas.get(i);
                SelectSheduleObject selectSheduleObject=new SelectSheduleObject();
                selectSheduleObject.setObjectName(displayArea.getDisplayName());
                selectSheduleObject.setObjectId(displayArea.getDisplayId());
                selectSheduleObject.setPosition(i);
                selectSheduleObject.setSelect(true);


                if (null!=displayArea.getExhibitionNeItems())
                {
                    for (int j=0;j<displayArea.getExhibitionNeItems().size();j++)
                    {

                        ExhibitionNeItem exhibitionNeItem=displayArea.getExhibitionNeItems().get(j);
                        SubSelectSheduleObject subSelectSheduleObject=new SubSelectSheduleObject();
                        subSelectSheduleObject.setObjectId(exhibitionNeItem.getExhibitionId());
                        subSelectSheduleObject.setObjectName(exhibitionNeItem.getExhibitionName());
                        subSelectSheduleObject.setSelectSheduleObject(selectSheduleObject);
                        subSelectSheduleObject.setSelect(true);
                        selectSheduleObject.addSubSelectSheduleObject(subSelectSheduleObject);
                    }
                }
                selectSheduleObjects.add(selectSheduleObject);

            }

        }else
        {
            for (int i = 0; i < displayAreas.size(); i++) {

                DisplayArea displayArea = displayAreas.get(i);
                SelectSheduleObject selectSheduleObject=new SelectSheduleObject();
                selectSheduleObject.setObjectName(displayArea.getDisplayName());
                selectSheduleObject.setObjectId(displayArea.getDisplayId());
                selectSheduleObject.setPosition(i);

                if (!TextUtils.isEmpty(selectIds)&&selectIds.contains(displayArea.getDisplayId()))
                {
                    selectSheduleObject.setSelect(true);
                }else
                {
                    selectSheduleObject.setSelect(false);
                }


                if (null!=displayArea.getExhibitionNeItems())
                {
                    for (int j=0;j<displayArea.getExhibitionNeItems().size();j++)
                    {

                        ExhibitionNeItem exhibitionNeItem=displayArea.getExhibitionNeItems().get(j);
                        SubSelectSheduleObject subSelectSheduleObject=new SubSelectSheduleObject();
                        subSelectSheduleObject.setObjectId(exhibitionNeItem.getExhibitionId());
                        subSelectSheduleObject.setObjectName(exhibitionNeItem.getExhibitionName());
                        subSelectSheduleObject.setSelectSheduleObject(selectSheduleObject);
                        if (selectSheduleObject.isSelect())
                        {
                            subSelectSheduleObject.setSelect(true);
                        }else
                        {
                            if (!TextUtils.isEmpty(selectIds)&&selectIds.contains(exhibitionNeItem.getExhibitionId()))
                            {
                                subSelectSheduleObject.setSelect(true);
                            }else
                            {
                                subSelectSheduleObject.setSelect(false);
                            }
                        }
                        selectSheduleObject.addSubSelectSheduleObject(subSelectSheduleObject);
                    }
                }
                selectSheduleObjects.add(selectSheduleObject);

            }
        }

    }

    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.confirm_btn:
                StringBuffer stringBuffer=new StringBuffer();
                if (isAllSelect)
                {
                    stringBuffer.append("ZALL");
                }else
                {
                    for (SelectSheduleObject selectSheduleObject:selectSheduleObjects)
                    {
                        if (selectSheduleObject.isSelect())
                        {
                            stringBuffer.append(selectSheduleObject.getObjectId()).append(",");
                        }else
                        {
                            if (null!=selectSheduleObject.getSubSelectSheduleObjects())
                            {
                                for (SubSelectSheduleObject subSelectSheduleObject:selectSheduleObject.getSubSelectSheduleObjects())
                                {
                                    if (subSelectSheduleObject.isSelect())
                                    {
                                        stringBuffer.append(subSelectSheduleObject.getObjectId()).append(",");
                                    }
                                }
                            }
                        }
                    }
                }

                Intent resultIntent=new Intent();
                resultIntent.putExtra("selectIds",stringBuffer.toString());
                setResult(RESULT_OK,resultIntent);
                finish();
                break;
            case R.id.all_select_state_layout:
                if (isAllSelect)
                {
                    isAllSelect=false;

                    allSelectImg.setImageResource(R.mipmap.kjg_menu_uncheck);
                    allSelectTxt.setTextColor(unSelectColor);
                    allSelectTxt.setBackgroundResource(R.drawable.kjg_item_display_bg1);

                    if (null!=selectSheduleObjects)
                    {
                        for (SelectSheduleObject selectSheduleObject:selectSheduleObjects)
                        {
                            selectSheduleObject.setSelect(false);

                            if (null!=selectSheduleObject.getSubSelectSheduleObjects())
                            {
                                for (SubSelectSheduleObject subSelectSheduleObject:selectSheduleObject.getSubSelectSheduleObjects())
                                {
                                    subSelectSheduleObject.setSelect(false);
                                }
                            }
                        }

                        selectSheduleObjectList.getAdapter().notifyDataSetChanged();

                    }



                }else
                {
                    isAllSelect=true;
                    allSelectImg.setImageResource(R.mipmap.kjg_menu_checked);
                    allSelectTxt.setTextColor(selectColor);
                    allSelectTxt.setBackgroundResource(R.drawable.kjg_item_display_bg2);

                    if (null!=selectSheduleObjects)
                    {
                        for (SelectSheduleObject selectSheduleObject:selectSheduleObjects)
                        {
                            selectSheduleObject.setSelect(true);

                            if (null!=selectSheduleObject.getSubSelectSheduleObjects())
                            {
                                for (SubSelectSheduleObject subSelectSheduleObject:selectSheduleObject.getSubSelectSheduleObjects())
                                {
                                    subSelectSheduleObject.setSelect(true);
                                }
                            }
                        }

                        selectSheduleObjectList.getAdapter().notifyDataSetChanged();

                    }
                }
                break;
        }

    }

    @Override
    public void isAllSelect(boolean isAllSelect) {

        this.isAllSelect=isAllSelect;

        if (isAllSelect)
        {
            allSelectImg.setImageResource(R.mipmap.kjg_menu_checked);
            allSelectTxt.setTextColor(selectColor);
            allSelectTxt.setBackgroundResource(R.drawable.kjg_item_display_bg2);
        }else
        {
            allSelectImg.setImageResource(R.mipmap.kjg_menu_uncheck);
            allSelectTxt.setTextColor(unSelectColor);
            allSelectTxt.setBackgroundResource(R.drawable.kjg_item_display_bg1);
        }
    }
}
